#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100020;
struct offlineNode{
	ll q,x;
}offline[MAXN];
ll b[MAXN];
ll n;
struct Node{
	ll l,r,val;
}T[MAXN << 2];
#define lc(p) p << 1
#define rc(p) p << 1 | 1
inline void push_up(ll root){
	T[root].val = T[lc(root)].val + T[rc(root)].val;
}
void build(ll root,ll l,ll r){
	T[root].l = l,T[root].r = r,T[root].val = 0;
	if(l == r)return;
	ll mid = (l + r) >> 1;
	build(lc(root),l,mid);
	build(rc(root),mid + 1,r);
}
void updata(ll root,ll x,ll p){
	if(T[root].l == T[root].r){
		T[root].val += p;
		return;
	}
	ll mid = (T[root].l + T[root].r) >> 1;
	if(x <= mid)updata(lc(root),x,p);
	else updata(rc(root),x,p);
	push_up(root);
}
ll query(ll root,ll l,ll r){
	if(T[root].l >= l && T[root].r <= r)return T[root].val;
	ll mid = (T[root].l + T[root].r) >> 1;
	ll ans = 0;
	if(l <= mid)ans += query(lc(root),l,r);
	if(r > mid)ans += query(rc(root),l,r);
	return ans;
}
ll requery(ll root,ll len){
	if(T[root].l == T[root].r)return T[root].l;
	if(len <= T[lc(root)].val)return requery(lc(root),len);
	else return requery(rc(root),len - T[lc(root)].val);
}
int main(void){
	ll act;
	scanf("%lld",&act);
	build(1,1,1e5 + 10);
	for(ll i=1;i<=act;i++){
		scanf("%lld%lld",&offline[i].q,&offline[i].x);
		if(offline[i].q!=4){
			b[++n]=offline[i].x;
		}
	}
	sort(b+1,b+n+1);
	n=unique(b+1,b+n+1)-(b+1);
//	for(ll i=1;i<=n;i++)cout<<b[i]<<" ";cout<<endl;
	for(ll i=1;i<=act;i++){
		ll x=offline[i].x;
		if(offline[i].q!=4){
			x=lower_bound(b+1,b+n+1,x)-b;
//			cout<<x<<endl;
		}
		ll ans;
		switch(offline[i].q){
			case 1:{
				updata(1,x,1);
				break;
			}
			case 2:{
				updata(1,x,-1);
				break;
			}
			case 3:{
				ans=query(1,1,x-1);
				ans++;
				printf("%lld\n",ans);
				break;
			}
			case 4:{
				ans=requery(1,x);
				printf("%lld\n",b[ans]);
				break;
			}
			case 5:{
				ans=query(1,1,x-1);
				ans=requery(1,ans);
				printf("%lld\n",b[ans]);
				break;
			}
			case 6:{
				ans=query(1,1,x);
				ans=requery(1,ans+1);
				printf("%lld\n",b[ans]);
				break;
			}
		}
	}
	return 0;
}
